package com.example.wangandroid.ui.login.ui.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.LogUtils
import com.example.wangandroid.adapter.ProjectAdapter
import com.example.wangandroid.data.model.ItemViewModel
import com.example.wangandroid.data.model.ProjectViewModel
import com.example.wangandroid.databinding.FragmentprojectdataBinding
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout


class ProjectFragmentRecyleFragment : Fragment() {

  private lateinit var mineViewModel: ProjectViewModel
  private var _binding: FragmentprojectdataBinding? = null
  private  var refreshLayout: RefreshLayout? = null
  private val binding get() = _binding!!
  private var adapter: ProjectAdapter? =null
  @SuppressLint("NotifyDataSetChanged")
  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    mineViewModel =
            ViewModelProvider(this).get(ProjectViewModel::class.java)
    _binding = FragmentprojectdataBinding.inflate(inflater, container, false)
    val root: View = binding.root
    return root
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initView()
  }
var cid:String = "0"
  fun initView(){
    mineViewModel._projectdataList.observe(viewLifecycleOwner, Observer {
      //具体项目诗句
      if (it != null) {
        LogUtils.d(it.toString())
        if(adapter==null){
          adapter= ProjectAdapter(this, it)
          _binding!!.ry.adapter = adapter
        }else{
          adapter!!.data = it
          adapter!!.notifyDataSetChanged()
        }
      }
      refreshLayout?.finishRefresh()
      refreshLayout?.finishLoadMore()
    })
    cid = requireArguments().getString("cid").toString()
    mineViewModel.getProjectDataList(cid)
    refreshLayout = _binding!!.refreshLayout
    refreshLayout!!.setRefreshHeader(ClassicsHeader(context))
    refreshLayout!!.setRefreshFooter(ClassicsFooter(context))
    refreshLayout!!.setOnLoadMoreListener {
      mineViewModel.getProjectDataListMore(cid)
    }
  }


  companion object {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private const val ARG_SECTION_NUMBER = "section_number"

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    @JvmStatic
    fun newInstance(sectionNumber: Int,cid:String): ProjectFragmentRecyleFragment {
      return ProjectFragmentRecyleFragment().apply {
        arguments = Bundle().apply {
          putInt(ARG_SECTION_NUMBER, sectionNumber)
          putString("cid", cid)
        }
      }
    }
  }
}